package com.xiaohao.server.parkmap.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaohao.server.parkmap.entity.Scenic;
import com.xiaohao.server.parkmap.mapper.ScenicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ScenicService {
    @Autowired
    private ScenicMapper scenicMapper;

    public Scenic queryById(Integer id) {
        return scenicMapper.selectById(id);
    }

    public Scenic queryOne(Scenic scenic) {
        return scenicMapper.selectOne(new QueryWrapper<>(scenic));
    }

    public List<Scenic> query(Scenic scenic) {
        LambdaQueryWrapper<Scenic> queryWrapper =
            new LambdaQueryWrapper<>(scenic).select(Scenic.class,
                tableFieldInfo -> !tableFieldInfo.getColumn().equals("content"));
        return scenicMapper.selectList(queryWrapper);
    }

    public Integer delete(Integer id) {
        return scenicMapper.deleteById(id);
    }

    public Integer save(Scenic scenic) {
        if (Objects.nonNull(scenic) && Objects.nonNull(scenic.getId())) {
            return scenicMapper.updateById(scenic);
        } else {
            return scenicMapper.insert(scenic);
        }
    }
}
